﻿Imports System.Data.OleDb
Public Class EditProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim sql As String = "SELECT * FROM UserProfile WHERE KitchenId=@j7"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("JamaicanConnectionString").ConnectionString)
            conn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@j7", User.Identity.Name)

            Dim dr = cmd.ExecuteReader()

            dr.Read()

            tb_FirstName.Text = dr("FirstName")
            tb_Surname.Text = dr("Surname")
            tb_Sex.Text = dr("Sex")

            Dim userDoB As DateTime = dr("DOB")

            ddl_year.SelectedValue = userDoB.Year
            ddl_month.SelectedValue = userDoB.Month
            ddl_day.SelectedValue = userDoB.Day
        End If

    End Sub

    Protected Sub btn_submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submit.Click
        Dim newFileName As String = ""

        If fup_Portrait.HasFile Then

            newFileName = Guid.NewGuid().ToString() & getSuffix(fup_Portrait.FileName)
            If fup_Portrait.HasFile Then
                fup_Portrait.SaveAs(Server.MapPath("picture") & "/" & newFileName)
            End If
        End If
        Dim userDoB As DateTime = New DateTime(CInt(ddl_year.SelectedValue), CInt(ddl_month.SelectedValue), CInt(ddl_day.SelectedValue))

        Dim sql As String

        If fup_Portrait.HasFile Then
            sql = "UPDATE UserProfile SET FirstName=@j1, Surname=@j2,Portrait=@j3, DOB=@j4, PrefaredTaste=@j5,Sex=@j6 WHERE KitchenId=@j7"
        Else
            sql = "UPDATE UserProfile SET FirstName=@j1, Surname=@j2, DOB=@j4, PrefaredTaste=@j5,Sex=@j6 WHERE KitchenId=@j7"
        End If
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("JamaicanConnectionString").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@j1", tb_FirstName.Text)
        cmd.Parameters.AddWithValue("@j2", tb_Surname.Text)
        If fup_Portrait.HasFile Then cmd.Parameters.AddWithValue("@j3", "picture/" & newFileName)
        cmd.Parameters.AddWithValue("@j4", userDoB.Date)
        cmd.Parameters.AddWithValue("@j5", tb_prefaredTaste.Text)
        cmd.Parameters.AddWithValue("@j6", tb_Sex.Text)
        cmd.Parameters.AddWithValue("@j7", User.Identity.Name)


        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
    End Sub

    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function
End Class